Skip to content

Commit 2d3a117

Browse files
committed
Change SumoBot to deleted user
* In messaging, when a user who received a message is deleted the sending users outbox will now show the message was to deleted user * When a message in your inbox is from a user who is deleted it will appear as being from deleted user
1 parent 64a23aa commit 2d3a117

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

kitsune/messages/handlers.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@ def on_user_deletion(self, user: User) -> None:
1313
When a user is deleted:
1414
- Delete their outbox messages
1515
- Keep inbox messages for other users and reassign them to SumoBot
16+
- Update outbox message recipients to SumoBot where the deleted user was a recipient
1617
"""
1718
sumo_bot = Profile.get_sumo_bot()
1819
InboxMessage.objects.filter(to=user).delete()
1920
InboxMessage.objects.filter(sender=user).update(sender=sumo_bot)
21+
22+
# Update outbox messages where the deleted user was a recipient
23+
outbox_messages = OutboxMessage.objects.filter(to=user)
24+
for message in outbox_messages:
25+
message.to.remove(user)
26+
message.to.add(sumo_bot)
27+
2028
OutboxMessage.objects.filter(sender=user).delete()

kitsune/messages/jinja2/messages/includes/macros.html

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212

1313
{% macro name_link(user=None, name=None) -%}
1414
{% if user -%}
15-
<a rel="nofollow" href="{{ profile_url(user) }}">{% if name %}{{ name }}{% else %}{{ display_name(user) }}{% endif %}</a>
15+
{% if user.profile.is_system_account %}
16+
{{ _('deleted user') }}
17+
{% else %}
18+
<a rel="nofollow" href="{{ profile_url(user) }}">{% if name %}{{ name }}{% else %}{{ display_name(user) }}{% endif %}</a>
19+
{% endif %}
1620
{%- else -%}
1721
{{ name if name else _('None') }}
1822
{%- endif %}
@@ -42,7 +46,11 @@
4246
{{ avatar_link(message.sender, default_avatar) }}
4347
</div>
4448
<div class="user from">
49+
{% if message.sender.profile.is_system_account %}
50+
{{ _('deleted user') }}
51+
{% else %}
4552
{{ name_link(message.sender) }} {{ datetimeformat(message.created) }}
53+
{% endif %}
4654
</div>
4755
</div>
4856
</section>
@@ -55,9 +63,13 @@
5563
{% if message.recipients_count > 0 %}
5664
<p><strong>{{ _('To') }}:</strong>
5765
{% set comma = joiner(', ') %}
58-
{% for user in message.to.all() -%}
66+
{% for user in message.to_users -%}
5967
{{ comma() }}
68+
{% if user.profile.is_system_account %}
69+
{{ _('deleted user') }}
70+
{% else %}
6071
{{ name_link(user) }}
72+
{% endif %}
6173
{% else %}
6274
{{ name_link(message.recipient) }}
6375
{% endfor %}

kitsune/messages/jinja2/messages/outbox.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,13 @@ <h1 class="sumo-page-heading">{{ title }}</h1>
3838
<div class="email-cell sent">{{ datetimeformat(message.created) }}</div>
3939
<div class="email-cell to">
4040
{% for user in message.to.all()[:1] -%}
41-
<a rel="nofollow" href="{{ profile_url(user) }}">
41+
{% if user.profile.is_system_account %}
42+
{{ _('deleted user') }}
43+
{% else %}
44+
<a rel="nofollow" href="{{ profile_url(user) }}">
4245
{{ user.profile.display_name }}
43-
</a>
46+
</a>
47+
{% endif %}
4448
{%- if message.recipients_count > 1 -%}, ...{% endif %}
4549
{%- endfor %}
4650
</div>

kitsune/messages/views.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ def inbox(request):
2626
.prefetch_related("sender__profile")
2727
)
2828
count = messages.count()
29-
3029
messages = paginate(request, messages, per_page=MESSAGES_PER_PAGE, count=count)
3130

3231
return render(
@@ -42,19 +41,25 @@ def read(request, msgid):
4241
was_new = message.unread
4342
if was_new:
4443
message.update(read=True)
44+
4545
initial = {"to": message.sender, "in_reply_to": message.pk}
4646
form = ReplyForm(initial=initial)
4747
response = render(
4848
request,
4949
"messages/read.html",
50-
{"message": message, "form": form, "default_avatar": settings.DEFAULT_AVATAR},
50+
{
51+
"message": message,
52+
"form": form,
53+
"default_avatar": settings.DEFAULT_AVATAR,
54+
},
5155
)
5256
return response
5357

5458

5559
@login_required
5660
def read_outbox(request, msgid):
5761
message = get_object_or_404(OutboxMessage, pk=msgid, sender=request.user)
62+
5863
return render(
5964
request,
6065
"messages/read-outbox.html",
@@ -179,6 +184,26 @@ def preview_async(request):
179184

180185

181186
def _add_recipients(msg):
187+
"""Process and attach recipient information to a message object.
188+
189+
This helper function calculates recipient counts and assigns recipient-related
190+
attributes to the message object for both individual users and groups.
191+
192+
Args:
193+
msg: An OutboxMessage object to process.
194+
195+
Returns:
196+
The modified message object with the following attributes set:
197+
- recipients_count: Number of individual recipients
198+
- to_groups_count: Number of group recipients
199+
- recipient: The single recipient (if exactly one), else None
200+
- recipient.is_bot_user: Boolean indicating if recipient is SumoBot
201+
- to_groups: List of recipient groups with prefetched profiles
202+
203+
Note:
204+
The function assumes msg.to and msg.to_group are valid related fields
205+
on the message object.
206+
"""
182207
# Set the counts based on the lists
183208
msg.recipients_count = msg.to.all().count()
184209
msg.to_groups_count = msg.to_group.all().count()

0 commit comments

Comments
 (0)